<template>
  <div class="register-container">
    <div class="register-box">
      <div class="logo-placeholder"></div>
      <h2>用户注册</h2>
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="register-username">账号:</label>
          <input type="text" id="register-username" v-model="username" required>
        </div>
        <div class="form-group">
          <label for="register-password">密码:</label>
          <input type="password" id="register-password" v-model="password" required>
        </div>
        <button type="submit" class="register-button">注册</button>
      </form>
      <p class="login-link">
        已有账号？ <router-link to="/login">登录</router-link>
      </p>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';

const username = ref('');
const password = ref('');
const router = useRouter();

const API_BASE_URL = 'http://localhost:9901/api';

const handleRegister = async () => {
  try {
    const response = await axios.post(`${API_BASE_URL}/user/register`, {
      username: username.value,
      password: password.value,
    });

    if (response.data && response.data.code === 200) {
      // 注册成功，可以根据后端返回的信息进行提示或跳转
      alert('注册成功！请登录。');
      router.push('/login'); // 跳转到登录页
    } else {
      console.error('注册失败:', response.data.message);
      // TODO: 显示错误消息给用户
      alert(response.data.message); // 简单提示
    }
  } catch (error) {
    console.error('注册异常:', error);
    // TODO: 显示错误消息给用户
    alert('注册过程中发生错误。'); // 简单提示
  }
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5;
}

.register-box {
  background-color: #fff;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  text-align: center;
  width: 100%;
  max-width: 400px;
}

.logo-placeholder {
  width: 80px;
  height: 80px;
  background-color: #007bff; /* 占位符颜色，之后替换为随机图标 */
  margin: 0 auto 20px;
  border-radius: 50%;
}

h2 {
  margin-bottom: 30px;
  color: #333;
}

.form-group {
  margin-bottom: 20px;
  text-align: left;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: bold;
  color: #555;
}

.form-group input[type="text"],
.form-group input[type="password"] {
  width: calc(100% - 22px); /* Adjusting for padding and border */
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 1em;
}

.register-button {
  width: 100%;
  padding: 12px;
  background-color: #28a745; /* Green color for register */
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1.1em;
  transition: background-color 0.3s ease;
  margin-top: 10px;
}

.register-button:hover {
  background-color: #218838;
}

.login-link {
  margin-top: 20px;
  font-size: 0.9em;
  color: #555;
}

.login-link a {
  color: #007bff;
  text-decoration: none;
}

.login-link a:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 600px) {
  .register-box {
    padding: 20px;
    margin: 20px;
  }

  .form-group input[type="text"],
  .form-group input[type="password"],
  .register-button {
    font-size: 0.9em;
    padding: 10px;
  }
}
</style> 